package com.haitaoit.chengquan.module.login.enterprise;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.login.RegistAgreementActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RegistEnterpriseActivity extends BaseActivity {
    @ViewInject(R.id.regist_phone)
    EditText regist_phone;
    @ViewInject(R.id.regist_getcode)
    TextView regist_getcode;
    @ViewInject(R.id.regist_code)
    EditText regist_code;
    @ViewInject(R.id.regist_pwd)
    EditText regist_pwd;
    @ViewInject(R.id.regist_pwd2)
    EditText regist_pwd2;
    @ViewInject(R.id.regist_cb)
    CheckBox regist_cb;


    private int time;
    private Timer timer = new Timer();
    private TimerTask wait;
    private String code = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_enterprise);
        ViewUtils.inject(this);


        // 广播 企业注册成功
//        IntentFilter mFilter = new IntentFilter();
//        mFilter.addAction("register_succ_qiye");
//        registerReceiver(mReceiver, mFilter);
    }

//    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            finish();
//        }
//    };
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        unregisterReceiver(mReceiver);
//    }

    @OnClick({R.id.left, R.id.regist_getcode, R.id.regist_xieyi, R.id.next_go})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.regist_getcode:// 验证码
                if (regist_getcode.getText().toString().equals("获取验证码")) {
                    if (Utils.isMobileNum(regist_phone.getText().toString())) {

                            if (hasNetWork()) {
                                httpGetCode();
                            }else {
                                return;
                            }
                            wait = new TimerTask() {
                                @Override
                                public void run() {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (time <= 0) {
                                                regist_getcode.setEnabled(true);
                                                regist_getcode.setFocusable(true);
                                                regist_getcode.setFocusableInTouchMode(true);
                                                regist_getcode.requestFocus();

                                                regist_getcode.setText("获取验证码");
                                                regist_phone.setEnabled(true);
                                                wait.cancel();
                                            } else {
                                                regist_getcode.setEnabled(false);
                                                regist_getcode.setFocusable(false);
                                                regist_getcode.setFocusableInTouchMode(false);
                                                regist_getcode.setText(time + "秒后重新获取");
                                            }
                                            time--;
                                        }
                                    });
                                }
                            };
                            time = 60;
                            timer.schedule(wait, 0, 1000);
                        } else {
                            ToastUtils.showToast("手机格式有误请重新输入！");
                        }

                }
                break;
            case R.id.regist_xieyi:// 协议
                intent.setClass(this, RegistAgreementActivity.class);
                startActivity(intent);
                break;
            case R.id.next_go:
                if (regist_code.getText().toString().equals("")) {
                    ToastUtils.showToast("请先获取验证码！");
                } else if (!regist_code.getText().toString().trim().equals(code)) {
                    ToastUtils.showToast("验证码有误，请重新输入！");
                } else if (regist_pwd.getText().toString().equals("")) {
                    ToastUtils.showToast("密码不能为空！");
                } else if (!Utils.isPassword(regist_pwd.getText().toString()) ||
                        regist_pwd.getText().toString().length() < 6 ||
                        regist_pwd.getText().toString().length() > 12) {
                    ToastUtils.showToast("新密码6-12位数字或字母！");
                } else if (!regist_pwd.getText().toString().equals(regist_pwd2.getText().toString())) {
                    regist_pwd2.setText("");
                    ToastUtils.showToast("两次密码输入不一样，请重新输入！");
                } else if (!regist_cb.isChecked()) {
                    ToastUtils.showToast("请同意注册协议！");
                } else {
                    intent.setClass(this, RegistEnterInfoActivity.class);
                    intent.putExtra("phone", regist_phone.getText().toString());
                    intent.putExtra("pwd", regist_pwd.getText().toString());
                    startActivity(intent);
                }
                break;
        }
    }

    // 获取手机验证码
    private void httpGetCode() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("phone", regist_phone.getText().toString()));
        list.add(new NameValuePairSign("type", "1"));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("phone", regist_phone.getText().toString());
        params.addQueryStringParameter("type", "1");
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.GetUser_phonecode, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            regist_phone.setEnabled(true);
                            time = 0;
                            return;
                        case 0:// 成功
                            regist_phone.setEnabled(true);
                            JSONObject js = new JSONObject(Response);
                            code = js.getString("code");
                            regist_phone.setEnabled(false);
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    time = 0;
                    regist_phone.setEnabled(true);
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                time = 0;
                ToastUtils.showToast("获取数据失败,请检查网络");
                regist_phone.setEnabled(true);
            }
        });
    }

}
